<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreUserRequest;
use App\Models\Cert;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Models\RoleUser;
use App\Models\User;
use App\Models\UserEdu;
use App\Models\UserMore;
use App\Models\UserWage;
use App\Services\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    protected $UserService;
    public function __construct(UserService $UserService)
    {
        $this->UserService = $UserService;
    }

    public function login(Request $Request)
    {
        $Request->validate([
            'idcard' => 'required|max:255',
            'password' => 'required|min:6',
        ]);

        $user = User::with('auths')->with('company')->where('idcard',$Request->idcard)->first();
        if(!$user){
            return response()->json(['code' => '404', 'msg' => '用户不存在']);
        }
        if($user->status != 1){
            return response()->json(['code' => '404', 'msg' => '用户没有登录权限']);
        }
        if(!$user->validateForPassportPasswordGrant($Request->password)){
            return response()->json(['code' => '404', 'msg' => '密码错误']);
        }
        $token = $user->createToken('api')->accessToken;
        $project = Project::select('id','title','status')->orderBy('created_at','desc')->first();
        return response()->json(['code' => '200', 'msg' => '登录成功','user'=>$user,'token'=>$token,'project'=>$project]);
    }

    public function register(Request $Request)
    {
        $POSTONLY = User::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input_more = $Request->more;
        $input['company_id'] = 1;
        $input['status'] = 0;
        $data = $this->UserService->insert($input,$input_more);
        return response()->json(['code' => '200', 'msg' => '注册成功，请等待审核','data'=>$data]);
    }



    public function index(Request $Request) {
        $search = $Request->search;
        $formal = $Request->formal;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->UserService->get($search,$formal);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function all(Request $Request) {
        $sql = User::select('id','name','mobile','bank','bank_number','bank_account')->where('id','>',1)->with('certs')->with('roles');
        $total = $sql->count();
        $data = $sql->orderBy('created_at','desc')->get();
        $roleUser = RoleUser::where('role_id',$Request->role_id)->get();
        foreach ($data as $k => $v) {
            $data[$k]->cert_id = 0;
            $isCert = $roleUser->where('user_id',$v->id)->first();
            if($isCert){
                $data[$k]->cert_id = $isCert->cert_id;
            }
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function userRoles(Request $Request) {
        $data = $this->UserService->userRoles($Request->user_id,$Request->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function getA(Request $Request) {
        $data = [
            'name' => env('INVOICE_NAME'),
            'tax_id' => env('INVOICE_TAX'),
            'address' => env('INVOICE_ADDRESS'),
            'mobile' => env('INVOICE_MOBILE'),
            'bank_number' => env('INVOICE_BANKNUMBER'),
            'bank_account' => env('INVOICE_BANKACCOUNT'),
            'bank' => env('INVOICE_BANK'),
            'email' => env('INVOICE_EMAIL'),
            'postal_code' => env('INVOICE_POSTALCODE'),
        ];
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = User::with('more')->with('certs')->with('edus')->where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function imprest(Request $Request) {
        $data = User::select('id','name','mobile')->with('imprestPrice')->where('id',Auth::user()->id)->first();
        $data->imprest_price_sum = 0;
        foreach ($data->imprestPrice as $key => $value) {
            $data->imprest_price_sum += $value->price;
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function showProjectUser(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = ProjectUser::where('project_id',$Request->project_id)->where('user_id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function edu(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = UserEdu::where('id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function certs(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Cert::where('user_id',$Request->id)->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    /**
     * 存储一个新用户。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreUserRequest $Request)
    {
        $POSTONLY = User::POSTONLY;
        $POSTONLY_MORE = UserMore::POSTONLY;
        $input = $Request->only($POSTONLY);
        // $input_more = $Request->only($POSTONLY_MORE);
        $input_more = $Request->more;
        $input['company_id'] = Auth::user()->company_id;
        $input['password'] = substr($Request->idcard,14,4);
        $data = $this->UserService->insert($input,$input_more);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(StoreUserRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = User::POSTONLY;
        // $POSTONLY_MORE = UserMore::POSTONLY;
        $input = $Request->only($POSTONLY);
        // $input_more = $Request->only($POSTONLY_MORE);
        $input_more = $Request->more;
        $input['id'] = $Request->id;
        // $input['password'] = $Request->password ?? '123456';
        $data = $this->UserService->update($input,$input_more);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function storeEdu(Request $Request)
    {
        $POSTONLY = UserEdu::POSTONLY;
        $input = $Request->only($POSTONLY);
        $data = $this->UserService->insertEdu($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function updateEdu(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = UserEdu::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->UserService->updateEdu($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function wage(Request $Request) {
        $sql = $this->UserService->getWage($Request->issue_date);
        $total = $sql->count();
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function storeWage(Request $Request)
    {
        $data = $this->UserService->insertWage($Request->issue_date);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function updateWage(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = UserWage::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $input['operate_id'] = Auth::user()->id;
        $data = $this->UserService->updateWage($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function deleteBind(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $user = User::find($Request->id);
        $data = $user->projects()->detach($Request->project_id);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function bindProjectRole(Request $Request)
    {
        if(!$Request->user_id){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        if(!$Request->role_id){
            return response()->json(['code' => '404', 'msg' => '请选择岗位']);
        }
        $data = $this->UserService->bindProjectRole($Request->input());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function userBindProject(Request $Request)
    {
        if(!$Request->user_id){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        if(!$Request->projects){
            return response()->json(['code' => '404', 'msg' => '请选择项目']);
        }
        $data = $this->UserService->userBindProject($Request->input());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function userBindProjectRole(Request $Request)
    {
        if(!$Request->user_id){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        if(!$Request->roles){
            return response()->json(['code' => '404', 'msg' => '请选择岗位']);
        }
        $data = $this->UserService->userBindProjectRole($Request->input());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function userBindProjectCert(Request $Request)
    {
        if(!$Request->user_id){
            return response()->json(['code' => '404', 'msg' => '请选择用户']);
        }
        if(!$Request->cert_id){
            return response()->json(['code' => '404', 'msg' => '请选择证书']);
        }
        if(!$Request->role_id){
            return response()->json(['code' => '404', 'msg' => '请选择岗位']);
        }
        $data = $this->UserService->userBindProjectCert($Request->input());
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function status(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = User::find($Request->id);
        if(!$data){
            return response()->json(['code' => '404', 'msg' => '记录不存在']);
        }
        $data = $this->UserService->status($data);
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = User::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function deleteEdu(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = UserEdu::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }

    public function attendance(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->UserService->getAttendance($search);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function payment(Request $Request) {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->UserService->getPayment($search);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }
}
